﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Mobile.Properties;
using SDT.Model;
using SDT.Mobile.Models;
using SDT.Lib.Paging;
using SDT.Lib.Common;
using System.Linq.Dynamic;
namespace SDT.Mobile.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Article/

        SDTDataContext db = new SDTDataContext();
        public ActionResult Details(string id)
        {
            var da = new ArticleViewModel();
            if (id != null && id != "")
            {
                var data = db.Articles.Where(e => e.ID.Equals(int.Parse(id))).OrderByDescending(e => e.ID).ToList();
                da.Articles = data.ToList();

            }
            else
            {
                var data = db.Articles.OrderByDescending(e => e.ID).ToList();             
                da.Articles = data.ToList();
            }
            da.ArticleCategorys = db.ArticleCategories.ToList();
            da.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            da.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            return View(da);
        }
        [ChildActionOnly]
        public ActionResult Lasted(int number, int? catid,int skip, string order, string orderDir)
        {
            var a = new ArticleViewModel();
            var db = new SDTDataContext();
            if (skip <= 0)
            {
                skip = 0;
            }
                var l = db.Articles
                            .Where(e => e.PubYN == true)
                            .Where(e => catid > 0 ? e.CatID == catid : true).OrderBy(string.Format("{0} {1}", order, orderDir)).Skip(skip).Take(number).ToList();
            a.Articles = l;
            a.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
            a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
            var tmp = SDT.Lib.Common.Globals.DefaultLogo.Remove(0, 1);
            a.DefaultUrl = Settings.Default.SiteRootUrl + tmp;
            return View(a);
        }
        /// <summary>
        /// Modified by: Vietlv  
        /// Date: 03/11/201
        /// </summary>
        
         public ActionResult Index(int? id,int? page)
        {
            var type = Request["ddlArticleCategory"] != null ? Utils.ParseInt(Request["ddlArticleCategory"].ToString()) : 0;
            
            if (id == null)
            {
                id = type;
                ViewData["aticlecat"] = type;
            }
            else if (id != null)
            {
                type = (int)id;
            }
            else { ViewData["aticlecat"] = id; }
            var Articlename = Request["Articlename"];
            ViewData["articlenam"] = Request["Articlename"]; ;
            var p = new ArticleViewModel();
            if (page == null) page = 1;
            int pageSize = 14;
            var childs = db.ArticleCategories.Where(e => e.ParentID == id).Select(e => e.ID).ToArray();
            if (Articlename != null && Articlename != "")
            {

                var l = db.Articles
                             //   .Where(e => id > 0 ? e.CatID == id : true)
                                //.Where(e => childs.Count > 0? childs.IndexOf(e.CatID) : true)
                                .Where(e => id > 0 ? (e.CatID == id || e.ArticleCategory.ParentID == id) : true )
                                //.Where(e => type > 0 ? e.CatID == type: true)
                                .Where(e => e.PubYN == true)
                                .Where(h => h.ArticleContent.IndexOf(Articlename) >=0 || h.Title.IndexOf(Articlename) >=0 )
                                .OrderByDescending(h => h.ID).ToPagedList(page.Value - 1, pageSize);
                p.Articlepage = l;
            }

            else
            {
                var l = db.Articles
                            //.Where(e => id > 0 ? e.CatID == id  : true)
                            .Where(e => id > 0 ? (e.CatID == id || e.ArticleCategory.ParentID == id) : true)
                            .Where(e => e.PubYN == true)
                            .OrderByDescending(h => h.ID).ToPagedList(page.Value - 1, pageSize);
                p.Articlepage = l;
            }

             p.ArticleCategorys = db.ArticleCategories
                                        .Where(e => e.ActiveYN == true)
                                        .ToList();
             p.ProductTypes = db.ProductTypes.ToList();
             p.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
             p.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
             var tmp = SDT.Lib.Common.Globals.DefaultLogo.Remove(0, 1); 
             p.DefaultUrl = Settings.Default.SiteRootUrl + tmp;
             ViewData["aticlecat"] = type;
            return View(p);
       
        }
         [ChildActionOnly]
         public ActionResult LastedArticle(int number, int? catid, int skip, string order, string orderDir)
         {
             var a = new ArticleViewModel();
             var db = new SDTDataContext();
             if (skip == 0)
             {
                 var l = db.Articles
                                .Where(e => e.PubYN == true)
                                .Where(e => catid > 0 ? e.CatID == catid : true)
                                .Where(c => number>=0? c.ID >number:true)
                                .OrderBy(string.Format("{0} {1}", order, orderDir)).Take(6).ToList();
                 a.Articles = l;
             }
             if (skip == 1) 
             {
                 var l = db.Articles
                                .Where(e => e.PubYN == true)
                                .Where(e => catid > 0 ? e.CatID == catid : true)
                                .Where(c => number >= 0 ? c.ID < number : true)
                                .OrderBy(string.Format("{0} {1}", order, orderDir)).Take(6).ToList();
                 a.Articles = l;
             }
             a.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
             a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
             return View(a);
         }
         [ChildActionOnly]
         public ActionResult Gets(int number, int skip, int catid, string title, string aorder, string aorderDir, string viewName, string controllerName, string actionList, string actionDetail)
         {
             var a = new ArticleListViewModel();
             var l = ArticleManager.GetList(catid, string.Empty, 0, 1, aorder, aorderDir, number, skip);
             ViewData["Title"] = title;
             a.Articles = l;
             a.RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix;
             a.ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath;
             a.Category = catid > 0 ? db.ArticleCategories.Where(e => e.ID == catid).SingleOrDefault() : null;
             a.ControllerName = controllerName;
             a.ActionList = actionList;
             a.ActionDetails = actionDetail;
             return PartialView(viewName, a);
         }
    }
}
